home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / EXAMPLES / splatlogo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  4.9 KB  |  227 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1996. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <GL/glut.h>
  11.  
  12. #define MAX_SPLATS 100
  13.  
  14. extern int logo_width;
  15. extern int logo_height;
  16. extern unsigned char logo_image[];
  17.  
  18. typedef struct _SplatInfo {
  19.   int x, y;
  20.   GLboolean alphaTest;
  21.   GLfloat xScale, yScale;
  22.   GLfloat scale[3];
  23.   GLfloat bias[3];
  24. } SplatInfo;
  25.  
  26. int winHeight;
  27. int numSplats = 0;
  28. SplatInfo splatConfig;
  29. SplatInfo splatList[MAX_SPLATS];
  30. SplatInfo splatDefault = {
  31.   0, 0,
  32.   GL_TRUE,
  33.   1.0, 1.0,
  34.   { 1.0, 1.0, 1.0 },
  35.   { 0.0, 0.0, 0.0 }
  36. };
  37.  
  38. void
  39. reshape(int w, int h)
  40. {
  41.   glViewport(0, 0, w, h);
  42.   glMatrixMode(GL_PROJECTION);
  43.   glLoadIdentity();
  44.   gluOrtho2D(0, w, 0, h);
  45.   glMatrixMode(GL_MODELVIEW);
  46.   winHeight = h;
  47. }
  48.  
  49. void
  50. renderSplat(SplatInfo *splat)
  51. {
  52.     glRasterPos2i(splat->x, splat->y);
  53.     if(splat->yScale >= 0)
  54.       glBitmap(0, 0, 0, 0, 0, -logo_height * splat->yScale, 0);
  55.     if(splat->xScale < 0)
  56.       glBitmap(0, 0, 0, 0, logo_width * -splat->xScale, 0, 0);
  57.     glPixelZoom(splat->xScale, splat->yScale);
  58.     glPixelTransferf(GL_RED_SCALE, splat->scale[0]);
  59.     glPixelTransferf(GL_GREEN_SCALE, splat->scale[1]);
  60.     glPixelTransferf(GL_BLUE_SCALE, splat->scale[2]);
  61.     glPixelTransferf(GL_RED_BIAS, splat->bias[0]);
  62.     glPixelTransferf(GL_GREEN_BIAS, splat->bias[1]);
  63.     glPixelTransferf(GL_BLUE_BIAS, splat->bias[2]);
  64.     if (splat->alphaTest) 
  65.       glEnable(GL_ALPHA_TEST);
  66.     else
  67.       glDisable(GL_ALPHA_TEST);
  68.     glDrawPixels(logo_width, logo_height, GL_RGBA,
  69.       GL_UNSIGNED_BYTE, logo_image);
  70. }
  71.  
  72. void
  73. display(void)
  74. {
  75.   int i;
  76.  
  77.   glClear(GL_COLOR_BUFFER_BIT);
  78.   for (i = 0; i < numSplats; i++) {
  79.     renderSplat(&splatList[i]);
  80.   }
  81.   glFlush();
  82. }
  83.  
  84. void
  85. mouse(int button, int state, int x, int y)
  86. {
  87.   if (button == GLUT_LEFT_BUTTON) {
  88.     if (state == GLUT_DOWN) {
  89.       if (numSplats < MAX_SPLATS) {
  90.         splatConfig.x = x;
  91.         splatConfig.y = winHeight - y;
  92.     renderSplat(&splatConfig);
  93.         splatList[numSplats] = splatConfig;
  94.         numSplats++;
  95.       } else {
  96.         printf("out of splats!\n");
  97.       }
  98.     }
  99.   }
  100. }
  101.  
  102. void
  103. mainSelect(int value)
  104. {
  105.   GLfloat rpos[4];
  106.   GLboolean valid;
  107.  
  108.   switch(value) {
  109.   case 0:
  110.     numSplats = 0;
  111.     glutPostRedisplay();
  112.     break;
  113.   case 1:
  114.     splatConfig = splatDefault;
  115.     break;
  116.   case 2:
  117.     splatConfig.xScale *= 1.25;
  118.     splatConfig.yScale *= 1.25;
  119.     break;
  120.   case 3:
  121.     splatConfig.xScale *= 0.75;
  122.     splatConfig.yScale *= 0.75;
  123.     break;
  124.   case 4:
  125.     splatConfig.xScale *= -1.0;
  126.     break;
  127.   case 5:
  128.     splatConfig.yScale *= -1.0;
  129.     break;
  130.   case 6:
  131.     splatConfig.alphaTest = GL_TRUE;
  132.     break;
  133.   case 7:
  134.     splatConfig.alphaTest = GL_FALSE;
  135.     break;
  136.   case 411:
  137.     glGetFloatv(GL_CURRENT_RASTER_POSITION, rpos);
  138.     glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &valid);
  139.     printf("Raster position (%g,%g) is %s\n",
  140.       rpos[0], rpos[1], valid ? "valid" : "INVALID");
  141.     break;
  142.   case 666:
  143.     exit(0);
  144.     break;
  145.   }
  146. }
  147.  
  148. void
  149. scaleBiasSelect(int value)
  150. {
  151.   int color = value >> 4;
  152.   int option = value & 0xf;
  153.  
  154.   switch(option) {
  155.   case 1:
  156.     splatConfig.bias[color] += 0.25;
  157.     break;
  158.   case 2:
  159.     splatConfig.bias[color] -= 0.25;
  160.     break;
  161.   case 3:
  162.     splatConfig.scale[color] *= 2.0;
  163.     break;
  164.   case 4:
  165.     splatConfig.scale[color] *= 0.75;
  166.     break;
  167.   }
  168. }
  169.  
  170. int
  171. glutScaleBiasMenu(int mask)
  172. {
  173.   int menu;
  174.  
  175.   menu = glutCreateMenu(scaleBiasSelect);
  176.   glutAddMenuEntry("+25% bias", mask | 1);
  177.   glutAddMenuEntry("-25% bias", mask | 2);
  178.   glutAddMenuEntry("+25% scale", mask | 3);
  179.   glutAddMenuEntry("-25% scale", mask | 4);
  180.   return menu;
  181. }
  182.  
  183. int
  184. main(int argc, char *argv[])
  185. {
  186.   int mainMenu, redMenu, greenMenu, blueMenu;
  187.  
  188.   glutInitWindowSize(680, 440);
  189.   glutInit(&argc, argv);
  190.   splatConfig = splatDefault;
  191.  
  192.   glutCreateWindow("splatlogo");
  193.  
  194.   glutReshapeFunc(reshape);
  195.   glutDisplayFunc(display);
  196.   glutMouseFunc(mouse);
  197.  
  198.   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  199.   glAlphaFunc(GL_GEQUAL, 0.5);
  200.   glDisable(GL_ALPHA_TEST);
  201.   glEnable(GL_DITHER);
  202.   glClearColor(1.0, 1.0, 1.0, 0.0);
  203.  
  204.   redMenu = glutScaleBiasMenu(0 << 4);
  205.   greenMenu = glutScaleBiasMenu(1 << 4);
  206.   blueMenu = glutScaleBiasMenu(2 << 4);
  207.  
  208.   mainMenu = glutCreateMenu(mainSelect);
  209.   glutAddMenuEntry("Reset splays", 0);
  210.   glutAddMenuEntry("Reset splat config", 1);
  211.   glutAddSubMenu("Red control", redMenu);
  212.   glutAddSubMenu("Green control", greenMenu);
  213.   glutAddSubMenu("Blue control", blueMenu);
  214.   glutAddMenuEntry("+25% zoom", 2);
  215.   glutAddMenuEntry("-25% zoom", 3);
  216.   glutAddMenuEntry("X flip", 4);
  217.   glutAddMenuEntry("Y flip", 5);
  218.   glutAddMenuEntry("Enable alpha test", 6);
  219.   glutAddMenuEntry("Disable alpha test", 7);
  220.   glutSetMenu(mainMenu);
  221.   glutAddMenuEntry("Query raster position", 411);
  222.   glutAddMenuEntry("Quit", 666);
  223.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  224.   glutMainLoop();
  225.   return 0; /* Never reached; make ANSI C happy. */
  226. }
  227.